//
//  BleDataTool.swift
//  MMTest
//
//  Created by liujianlong on 2022/9/21.
//

import UIKit
import Foundation
import CoreBluetooth
#if canImport(TCC_String)
import TCC_String
#endif
#if canImport(TCC_Extension)
import TCC_Extension
#endif

public typealias writeBlock = (String?,
                               String?,
                               CBCharacteristicWriteType?,
                               ((WriteTask)->Void)?)->WriteTask?

//MARK: - 蓝牙协议指令处理中心协议<<CCBLEData_protocol>>
/// 蓝牙协议指令处理中心协议
public protocol CCBLEData_protocol{
    var head:[UInt8]{get}
    var cmd:[UInt8]{get}
    var ack:[UInt8]{get}
    var hex:[UInt8]{get}
    var data:Data{get}
    var service_uuidString:String{get}
    var write_char_uuidString:String{get}
    var read_char_uuidString:String{get}
    var write:WriteTask?{get}
    func write(service_uuid:String,
               write_char_uuid:String,
               type:CBCharacteristicWriteType,
               completion:((WriteTask)->Void)?)->WriteTask?
    var errorBlock : (()->Void)? {get}
}

/// 蓝牙协议指令处理中心协议<写入数据>
public extension CCBLEData_protocol {
    var write : WriteTask?{
        if BLE.connectedPeripherals.count <= 0{
            errorBlock?()
            return nil
        }
        let task = BLE.write(data: data,
                             service_uuid:service_uuidString,
                             characteristic_uuid:write_char_uuidString,
                             type:.withoutResponse,
                             completion: nil)
        return task
    }
    
    func write(service_uuid:String = "",
               write_char_uuid:String = "",
               type:CBCharacteristicWriteType = .withoutResponse,
               completion:((WriteTask)->Void)? = nil)->WriteTask?{
        if BLE.connectedPeripherals.count <= 0{
            errorBlock?()
            return nil
        }
        let task = BLE.write(data: data,
                             service_uuid:service_uuid == "" ? service_uuidString : service_uuid,
                             characteristic_uuid: write_char_uuid == "" ? write_char_uuidString : write_char_uuid,
                             type: type,
                             completion: completion)
        return task
    }
}


//MARK: - 遵守<<CCBLEData_protocol>>的示例
internal enum BLEData : CCBLEData_protocol{
    var errorBlock: (() -> Void)?{
        return {}
    }
    
    /// 测试例子
    case test(_ on:Bool)
}

internal extension BLEData{
    var head:[UInt8]{
        return [0xAA,0x55]
    }
    
    var cmd : [UInt8]{
        switch self{
        case .test:
            return [0x00,0x00]
        default:
            return [UInt8]()
        }
    }
    
    var ack:[UInt8]{
        switch self{
        case .test:
            return [0x00,0x00]
        default:
            return [UInt8]()
        }
        
    }
    
    
    var hex:[UInt8]{
        switch self{
        case .test:
            return [0x00,0x00]
        default:
            return [UInt8]()
        }
    }
    
    var data:Data{
        let str = self.head + self.cmd + self.hex
        return Data(str)
    }
    
    var service_uuidString:String{
        return "FF00"
    }
    
    var write_char_uuidString:String{
        return "FF01"
    }
    var read_char_uuidString:String{
        return "FF02"
    }
}


//MARK: - 数据更新的示例
/// 蓝牙数据更新协议
@objc public protocol CCBLEData_update_protocol{
    @objc optional func handleData(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
}




